import isFunctionComponent from './isFunctionComponent';
import mountNativeElement from './mountNativeElement';
import isFunction from './isFunction';

export default function mountComponent(virtualDOM, container, oldDOM) {
    let nextVirtualDom = null;
    // 判断组件是类组件还是函数组件
    if (isFunctionComponent(virtualDOM)) {
        nextVirtualDom = buildFunctionComponent(virtualDOM);
    } else {
        nextVirtualDom = buildClassComponent(virtualDOM);
    }
    if (isFunction(nextVirtualDom)) {
        mountComponent(nextVirtualDom, container, oldDOM)
    } else {
        mountNativeElement(nextVirtualDom, container, oldDOM);
    }
};

function buildFunctionComponent(virtualDOM) {
    return virtualDOM.type(virtualDOM.props || {});
}

function buildClassComponent(virtualDOM) {
    // 获取类组件实例
    const instance = new virtualDOM.type(virtualDOM.props || {});
    const nextVirtualDom = instance.render();
    nextVirtualDom.component = instance;
    return nextVirtualDom;
};